package cn.yr.system.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.common.core.constant.UserConstants;
import cn.yr.common.core.domain.entity.SysRole;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.exception.exception.ParameterException;
import cn.yr.common.exception.ret.Ret;
import cn.yr.common.security.annotation.PreAuthorize;
import cn.yr.system.service.ISysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
* @title SysRoleController
* @description 角色信息
* @author Lang 1102076808@qq.com
* @date 2020/7/12 10:39
*/

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

	@Autowired
	private ISysRoleService roleService;

	@RequestMapping("/list")
	@PreAuthorize(hasPermi = "system:role:list")
	public Ret list(@RequestBody PageVo<SysRole> pageVo) {
		Map<String, Object> params = pageVo.getParams();
		QueryWrapper<SysRole> wrapper  = new QueryWrapper<>();
		wrapper.orderByAsc("role_sort");
		wrapper.like(ObjectUtil.isNotEmpty(params.get("roleName")),"role_name",params.get("roleName"));
		wrapper.like(ObjectUtil.isNotEmpty(params.get("roleKey")),"role_key",params.get("roleKey"));
		wrapper.eq(ObjectUtil.isNotEmpty(params.get("status")),"status",params.get("status"));
		wrapper.gt(ObjectUtil.isNotEmpty(params.get("beginTime")),"create_time",params.get("beginTime"));
		wrapper.lt(ObjectUtil.isNotEmpty(params.get("endTime")),"create_time",params.get("endTime"));
		PageVo<SysRole> page = roleService.page(pageVo,wrapper);
		return Ret.success(page);
	}


	/**
	 * 根据角色编号获取详细信息
	 */
	@PreAuthorize(hasPermi = "system:role:query")
	@GetMapping(value = "/{roleId}")
	public Ret getInfo(@PathVariable Long roleId) {
		return Ret.success(roleService.selectRoleById(roleId));
	}

	/**
	 * 新增角色
	 */
	@PreAuthorize(hasPermi = "system:role:add")
	@RequestMapping("/add")
	public Ret add(@Validated @RequestBody SysRole role) {
		if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
			return Ret.error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
			return Ret.error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
		}
		return Ret.success(roleService.insertRole(role));

	}

	/**
	 * 修改保存角色
	 */
	@PreAuthorize(hasPermi = "system:role:edit")
	@RequestMapping("/edit")
	public Ret edit(@Validated @RequestBody SysRole role) {
		//roleService.checkRoleAllowed(role);
		if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
			return Ret.error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
			return Ret.error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
		}
		return Ret.success(roleService.updateRole(role));
	}

	/**
	 * 修改保存数据权限
	 */
	@PreAuthorize(hasPermi = "system:role:edit")
	@RequestMapping("/dataScope/edit")
	public Ret dataScope(@RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		return Ret.success(roleService.authDataScope(role));
	}

	/**
	 * 状态修改
	 */
	@RequestMapping("/changeStatus")
	@PreAuthorize(hasPermi = "system:role:edit")
	public Ret changeStatus(@RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		return Ret.success(roleService.updateRoleStatus(role));
	}

	/**
	 * 删除角色
	 */
	@PreAuthorize(hasPermi = "system:role:remove")
	@RequestMapping("/remove/{roleIds}")
	public Ret remove(@PathVariable Long[] roleIds) {
		for (Long roleId : roleIds) {
			roleService.checkRoleAllowed(new SysRole(roleId));
			SysRole role = roleService.selectRoleById(roleId);
			if (roleService.countUserRoleByRoleId(roleId) > 0) {
				throw new ParameterException(String.format("%1$s已分配,不能删除", role.getRoleName()));
			}
		}
		return Ret.success(roleService.removeByIds(Arrays.asList(roleIds)));
	}

	/**
	 * 获取角色选择框列表
	 */
	@PreAuthorize(hasPermi = "system:role:list")
		@GetMapping("/optionSelect")
	public Ret optionSelect() {
		return Ret.success(roleService.selectRoleAll());
	}
}